package com.heima.kafka.sample;


import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.TimeWindows;
import org.apache.kafka.streams.kstream.ValueMapper;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

/**
 * 流式处理
 */

public class KafkaStreamQuickStart {
    public static void main(String[] args) {
        //2.配置kafka
        Properties prop = new Properties();
        prop.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "47.97.188.241:9092");
        prop.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        prop.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        prop.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-quickstart");

        //3.stream构建器
        StreamsBuilder streamBuilder = new StreamsBuilder();
        //流式计算
        streamProcessor(streamBuilder);

        //1.创建kafka流对象
        KafkaStreams kafkaStreams = new KafkaStreams(streamBuilder.build(), prop);

        //4.开启流式计算
        kafkaStreams.start();
    }

    /**
     * 流式计算
     *消息的内容：hello kafka  hello itcast
     * @param streamBuilder
     */
    private static void streamProcessor(StreamsBuilder streamBuilder) {
        //创建kStream对象并指定从哪个主题中获取消息
        KStream<String, String> stream = streamBuilder.stream("itcast-topic-input");

        //处理消息的value
        stream.flatMapValues(new ValueMapper<String, Iterable<?>>() {
                    @Override
                    public Iterable<String> apply(String value) {
                        String[] split = value.split(" ");
                        return Arrays.asList(split);
                    }
                })
                //按照value进行聚合处理
                .groupBy((key, value) -> value)
                //时间窗口
                .windowedBy(TimeWindows.of(Duration.ofSeconds(10)))
                //统计单词个数
                .count()
                //转换为KStream
                .toStream()
                .map((key, value) -> {
                    System.out.println("\033[32m" + "key : " + key + ", value : " + value);
                    return new KeyValue<>(key.key().toString(), value.toString());
                })
                //发送消息
                .to("itcast-topic-out");
    }
}
























